package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;

import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    CheckItemController(){
        System.out.println("CheckItemController 执行了");
    }
    @Reference
   private CheckItemService itemService;
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        System.out.println(11);
        try{
             itemService.add(checkItem);
             return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = itemService.findPage(queryPageBean);
              return pageResult;
    }

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){

        Boolean bool = itemService.deleteById(id);
        if (bool){
        return  new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
        return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    @RequestMapping("/show")
    public CheckItem show(Integer id){
        CheckItem checkItem =itemService.show(id);
        return checkItem;
    }

    @RequestMapping("/modify")
    public Result modify(@RequestBody CheckItem checkItem){
        Boolean bool =itemService.modify(checkItem);
        if(bool){
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        }else{
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }


//    查询所有的CheckItems
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckItem> checkItems = itemService.findAll();
            return new Result(true,null,checkItems);
        }catch (Exception e){
            return new Result(false,null);
        }
    }
}
